package com.itstudio.portal.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itstudio.portal.entity.Article;
import com.itstudio.portal.entity.Project;
import com.itstudio.portal.entity.RestResponse;
import com.itstudio.portal.entity.dto.ArticleDto;
import com.itstudio.portal.mapper.ArticleMapper;
import com.itstudio.portal.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文章管理Controller
 * 
 * @author 陈冲
 * @date 2024-11-16
 */
@RestController
@RequestMapping("/api/article")
public class ArticleController
{
    @Autowired
    private IArticleService articleService;

    @Autowired
    private ArticleMapper articleMapper;
    /**
     * 查询文章管理列表
     */

    @PostMapping("/list")
    public RestResponse<IPage<Article>> list(@RequestBody ArticleDto articleDto)
    {
        Page<Article> page = new Page<>();
        page.setSize(articleDto.getPageSize());        // 页面容量
        page.setCurrent(articleDto.getPageNumber());   // 当前页码
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Article::getCategoryId,articleDto.getCategoryId());

        //设置了文章的查询条件只查询已发布的
        queryWrapper.eq(Article::getStatus,0);

        queryWrapper.select().orderByDesc(Article::getCreateTime);
        // 执行分页查询
        IPage<Article> result = articleMapper.selectPage(page,queryWrapper);

        return RestResponse.success(result);


    }

    @GetMapping("/list/{id}")
    public RestResponse selectArticleById(@PathVariable Long id){
        Article article = articleService.selectArticleById(id);
        return RestResponse.success(article);

    }







}
